// 处理class = middle 的块元素在屏幕中央
function setMiddleBlock() {
    var middles = $(".middle");
    $.each(middles, function (i, n) {
        var selfHeight = $(n).height();
        var clientHeight = $(window).height();
        var marginTop = Math.round(clientHeight / 2 - selfHeight / 2);
        if (marginTop < 0) {
            marginTop = 0;
        }
        $(n).css("top", marginTop + 'px');
    })
}

// 加载头、脚、左边栏和右边栏
function initHeaderAndFooter() {
    $("#header").load("../frame/header.html");
    $("#footer").load("../frame/footer.html");
    $("#left").load("../frame/left.html");
    $("#right").load("../frame/right.html");
}

// 保存BootstrapTable关于列显示隐藏的设定项至localStorage
function saveBootstrapTableColumnVisibleSetting() {
    // 查找窗口内的每个bootstrap-table
    var key = window.location.host + window.location.pathname + "/bootstrap-table/column";
    $("table[data-toggle=table]").each(function () {
        var tableId = $(this).attr("id");

        var showColumns = $(this).bootstrapTable('getVisibleColumns');
        var showColumnsFields = "";
        for (var i = 0; i < showColumns.length; i++) {
            showColumnsFields += showColumns[i].field + ",";
        }
        window.localStorage.setItem(key + "/" + tableId + "/show", showColumnsFields.substring(0, showColumnsFields.length - 1));

        var hileColumns = $(this).bootstrapTable('getHiddenColumns');
        var hideColumnsFields = "";
        for (var j = 0; j < hileColumns.length; j++) {
            hideColumnsFields += hileColumns[j].field + ",";
        }
        window.localStorage.setItem(key + "/" + tableId + "/hide", hideColumnsFields.substring(0, hideColumnsFields.length - 1));
    });
}

// 加载BootstrapTable关于列显示隐藏的设定项
function getBootstrapTableColumnVisibleSetting() {
    var key = window.location.host + window.location.pathname + "/bootstrap-table/column";
    $("table[data-toggle=table]").each(function () {
        var tableId = $(this).attr("id");
        var showColumnsFields = window.localStorage.getItem(key + "/" + tableId + "/show");
        var showColumns = showColumnsFields.split(",");
        for (var i = 0; i < showColumns.length; i++) {
            $(this).bootstrapTable('showColumn', showColumns[i]);
        }

        var hideColumnsFields = window.localStorage.getItem(key + "/" + tableId + "/hide");
        var hileColumns = hideColumnsFields.split(",");
        for (var j = 0; j < hileColumns.length; j++) {
            $(this).bootstrapTable('hideColumn', hileColumns[i]);
        }
    });
}

// 获取url参数值
function getUrlParam (paramName) {
    var rtnValue;
    var url = window.location.href;
    url = url.substring(url.indexOf("?") + 1, url.length);
    url = url.split("&");
    for (var i = 0; i < url.length; i++) {
        if (url[i].split("=")[0] === paramName ) {
            rtnValue = url[i].split("=")[1];
            rtnValue = decodeURIComponent(rtnValue);
            break;
        }
    }
    return rtnValue;
}

// 对Date的扩展，将 Date 转化为指定格式的String
Date.prototype.format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
};

function dateFormate_yyyyMMdd(value, row ,index) {
    return new Date(value).format("yyyy-MM-dd");
}

function dateFormate_yyyyMMddHHmmss(value, row ,index) {
    return new Date(value).format("yyyy-MM-dd hh:mm:ss");
}

function dateFormate_yyyyMMddHHmmssS(value, row ,index) {
    return new Date(value).format("yyyy-MM-dd hh:mm:ss.S");
}
//  ===============================================>>> 下面这三个方法放在最后面

// window窗口大小变化
$(window).resize(function () {
    setMiddleBlock();
});

// 网页关闭时间
$(window).unload(function () {
    saveBootstrapTableColumnVisibleSetting();
});

// 加载完成事件
$(function () {
    setMiddleBlock();
    initHeaderAndFooter();
    getBootstrapTableColumnVisibleSetting();
});


